package com.acme.task1;

import org.junit.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class CalculatorTest {
    final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

    @Before
    public void setUpStreams() {
        System.setOut(new PrintStream(outContent));
        System.setErr(new PrintStream(errContent));
    }

    @After
    public void cleanUpStreams() {
        System.setOut(null);
        System.setErr(null);
    }

    @Test
    public void testAdd() {
        Calculator.main(new String[] {"2", "+", "2"});
        assert(outContent.toString().equals("4.0\n"));
    }

    @Test
    public void testSub() {
        Calculator.main(new String[] {"4", "-", "1.9"});
        assert(outContent.toString().equals("2.1\n"));
    }

    @Test
    public void testMul() {
        Calculator.main(new String[] {"3", "*", "7"});
        assert(outContent.toString().equals("21.0\n"));
    }

    @Test
    public void testDiv() {
        Calculator.main(new String[] {"2", "/", "2"});
        assert(outContent.toString().equals("1.0\n"));
    }

    @Test
    public void testDivZero() {
        Calculator.main(new String[] {"2", "/", "0"});
        assert(errContent.toString().equals("division by zero\n"));
    }

    @Test
    public void testNumArgs() {
        Calculator.main(new String[2]);
        assert(errContent.toString().equals("wrong number of arguments\n"));
    }

    @Test
    public void testOperands() {
        Calculator.main(new String[] {"xs", "+", "3"});
        assert(errContent.toString().equals("wrong operands\n"));
    }

    @Test
    public void testOeration() {
        Calculator.main(new String[] {"7", ">", "3"});
        assert(errContent.toString().equals("wrong operation\n"));
    }
}



